<template>
	<!-- <view class="login">
		<view class="login-container">
			<view class="login-img">
				<image src="../../static/assets/img/login.png" mode="widthFix"></image>
			</view>
			<view>
				<button class="btn-wechat" open-type="getPhoneNumber">
					<image src="../../static/assets/img/wechatIcon.png" class="btn-icon"></image>
					微信一键授权
				</button>
			</view>
		</view>
	</view> -->
</template>

<script>
	// pages/login/index.js
	import util from './../../utils/util.js';
	import api from './../../utils/api.js';
	export default {
		data() {
			return {
				weixinCode: '',

				//是否是扫描特制二维码进入-目的是登记外部车辆管理
				isFromScanCode: false,

				unionId: ''
			};
		}
		/**
		 * 生命周期函数--监听页面加载
		 */
		,
		onLoad: function(options) {
			let that = this;
			if (options.unionId) {
				const unionId = options.unionId;
				uni.setStorageSync('carunionId',options.unionId)
				this.setData({
					unionId: unionId //二维码携带的通道等参数
				});
			}
			if(options.scancode){
				this.getPhoneNumber(options)
			}else{
				jWeixin.miniProgram.reLaunch({
					url: "/pages/fromScanCode/fromScanCode"
				})
			}
			// console.log('onload');
			// let that = this;
			// uni.login({
			// 	success(res) {
			// 		that.setData({
			// 			weixinCode: res.code
			// 		});
			// 	}
			// });
			// /***
			//  * 扫二维码进来的逻辑：
			//  */


		},
		/**
		 * 生命周期函数--监听页面初次渲染完成
		 */
		onReady: function() {},
		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow: function() {},
		/**
		 * 生命周期函数--监听页面隐藏
		 */
		onHide: function() {},
		/**
		 * 生命周期函数--监听页面卸载
		 */
		onUnload: function() {},
		/**
		 * 页面相关事件处理函数--监听用户下拉动作
		 */
		onPullDownRefresh: function() {},
		/**
		 * 页面上拉触底事件的处理函数
		 */
		onReachBottom: function() {},
		/**
		 * 用户点击右上角分享
		 */
		onShareAppMessage: function() {},
		methods: {
			goLogin() {
				uni.navigateTo({
					url: '../phone/index'
				});
			},

			getPhoneNumber(e) {
				let that = this;
				if (e) {
					let param = {
						code: e.scancode,
						nikeName: '用户_' + util.randomWord(),
						encryptedData: e.encryptedData + "==",
						iv: e.iv + "==",
						url: ''
					};
					uni.showLoading({
						title: '加载中'
					});
					util.request(api.qrCodeGetPhone, param, 'POST')
						.then((res) => {
							if (res.success) {
								uni.hideLoading();
								uni.setStorage({
									key: 'AUTHORIZATION',
									data: res.data.busiInfo.token
								}); //token
								param.unionId = uni.getStorageSync('carunionId')
								param.url = ''
								param.openId = res.data.busiInfo.openId;
								// param.encryptedData = JSON.stringify(param.encryptedData)
								uni.setStorageSync('getPhonePar',param)
								uni.setStorageSync('phoneNumber',res.data.busiInfo.phoneNumber)
								uni.navigateTo({
									url: '/pages/externalCarCheck/externalCarCheck?encryptedData=' + encodeURI(e.encryptedData)
								}); 
							} else {
								uni.showToast({
									title: res.msg,
									icon: 'none'
								});
							}
						})
						.catch((err) => {
							uni.hideLoading();
							let that = this;
							uni.login({
								success(res) {
									that.setData({
										weixinCode: res.code
									});
								}
							});
							uni.showToast({
								title: '接口出错了，请重试',
								icon: 'none'
							});
						});
				}
			}
		}
	};
</script>
<style>
	@import './fromScanCode.css';
</style>
